package com.kgc.controller.user;

import com.kgc.common.Result;
import com.kgc.entity.User;
import com.kgc.service.interf.user.UserService;
import com.kgc.util.JwtUtil;
import com.kgc.util.SendEmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author hyj
 * @time 2024/4/2 11:14
 * @describe
 */

@RestController
@RequestMapping("/apis/user")
public class UserController {

    private static String codePass;

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public String register(@RequestBody User user) throws Exception {
        user.setUsername(user.getUsername());
        user.setPassword(user.getPassword());
        user.setEmail(user.getEmail());
        user.setCreatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int i = userService.register(user);
        if (i == 2) {
            return Result.success("0", "注册成功");
        } else if (i == 10) {
            return Result.success("10", "该用户名已存在");
        } else if (i == 20) {
            return Result.success("20", "该邮箱已被注册");
        }
        return Result.success("-1", "注册失败");
    }

    @PostMapping("/login")
    public String login(@RequestBody User user) throws Exception {
        User login = userService.login(user.getUsername(), user.getPassword());
        if (login != null && login.getUserId().equals(-1)) {
            return Result.success("2", "用户名或邮箱暂未注册，登录失败");
        } else if (login != null && login.getUserId().equals(-2)) {
            return Result.success("1", "密码错误，登录失败");
        } else if (login != null && login.getUserId().equals(-3)) {
            return Result.success("4", "该用户已被禁用，登录失败");
        } else if (login != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", login.getUserId());
            map.put("username", login.getUsername());
            login.setToken(JwtUtil.generateJwt(map));
            return Result.success("0", "登录成功", login);
        }
        return Result.success("3", "权限不对应，登录失败");
    }


    @PostMapping("/resetPassword")
    public String resetPassword(@RequestBody User user, String code) throws Exception {
        if (code.equals(codePass)) {
            int i = userService.updateUser(user.getEmail(), user.getPassword());
            if (i == 1) {
                return Result.success("0", "密码修改成功");
            }
            return Result.success("-1", "密码修改失败");
        }
        return Result.success("200", "验证码错误");
    }

    @PostMapping("/send")
    public String sendEmail(String email) {
        // 判断该邮箱是否存在
        Integer exist = userService.isExist(email);
        if (exist != null) {
            // 实例化一个发送邮件的对象
            SendEmailUtil mySendMail = new SendEmailUtil();
            //生成六位数验证码
            String captcha = String.valueOf(new Random().nextInt(899999) + 100000);
            //邮件主题
            new SimpleMailMessage().setSubject("验证码");
            //邮件内容
            new SimpleMailMessage().setText("验证码:" + captcha);
            mySendMail.sendMail(email, "您的验证码为：" + captcha);
            codePass = captcha;
            return Result.success("0", "验证码已发送");
        }
        return Result.success("10", "该邮箱暂未注册");
    }
}
